/*
 * @Author: wuxianyu
 * @Date:   2017-02-16 09:31:28
 * @Last Modified by:   zrug
 * @Last Modified time: 2017-02-26 17:01:00
 */
angular.module('app')
  .directive('uploadimgregisterreadonly', function($compile, ngDialog, uiLoad) {
    return {
      restrict: 'E',
      templateUrl: 'js/module/upload-direct/uploadtestRegisterReadOnly.html',
      replace: true
    };
  })
  .controller('fileUploadCtrl1', ['$element', '$scope', 'FileUploader', 'uploadAddress2', 'uploadprefix', 'signature', '$http', function($element, $scope, FileUploader, uploadAddress2, uploadprefix, signature, uploadConf, $http) {
    var uploader = $scope.uploader = new FileUploader({
      url: uploadAddress2
        //url: "http://139.196.38.189:8073/uploadFileSave.json"
    });

    $scope.uploadprefix = uploadprefix;

    $scope.tab = 0;

    $scope.setTab = function(newTab) {
      $scope.tab = newTab;
      $scope.selectTab = true;
    };

    $scope.isSet = function(tabNum) {
      return $scope.tab === tabNum;
    };

    $scope.add = function() {
      $scope.column2 = uploadConf.column2;
      $scope.maxImgNum = uploadConf.maxImgNum;
      $scope.imgNew = uploadConf.imgNew;
    };
    $scope.change = function() {
      uploadConf.column2 = $scope.column2;
      uploadConf.maxImgNum = $scope.maxImgNum;
      uploadConf.imgNew = $scope.imgNew;
    };

    uploader.onWhenAddingFileFailed = function(item /*{File|FileLikeObject}*/ , filter, options) {
      //console.info('onWhenAddingFileFailed', item, filter, options);
    };
    uploader.onAfterAddingFile = function(fileItem) {
      //console.info('onAfterAddingFile', fileItem);
      uploader.uploading = 1;
    };
    uploader.onAfterAddingAll = function(addedFileItems) {
      //console.info('onAfterAddingAll', addedFileItems);
    };
    uploader.onBeforeUploadItem = function(item) {
      console.log(uploadAddress2);
      // console.log(item._file);
      var uuidTMEP = uuid();
      item.formData = [{
        'fileName': uuidTMEP
      }];
      console.log(item.formData);
      return item.formData

      $scope.uploading = 1;
    };
    uploader.onProgressItem = function(fileItem, progress) {
      // console.info('onProgressItem', fileItem, progress);
    };
    uploader.onProgressAll = function(progress) {
      //console.info('onProgressAll', progress);
    };
    uploader.onSuccessItem = function(fileItem, response, status, headers) {
      uploader.uploading = 0;
      //console.info('onSuccessItem', fileItem, response, status, headers);
      // console.info('uploader', uploader);
    };
    uploader.onErrorItem = function(fileItem, response, status, headers) {
      //console.info('onErrorItem', fileItem, response, status, headers);
    };
    uploader.onCancelItem = function(fileItem, response, status, headers) {
      //console.info('onCancelItem', fileItem, response, status, headers);
    };
    uploader.onCompleteItem = function(fileItem, response, status, headers) {

      if (response && response.res && response.res.data && response.res.data.fileAccessPath) {
        if ($scope.indexSelect != -1) {
          $scope.imgNew[$scope.typeSelect].value.splice($scope.indexSelect, 1, response.res.data.fileAccessPath);
        } else {
          $scope.imgNew[$scope.typeSelect].value.push(response.res.data.fileAccessPath);
        }
      }
      $('#uploaderFileInput').val("");

    };

    $scope.remove = function(index, indexParent) {
      $scope.imgNew[indexParent].value.splice(index, 1);
    }
    $scope.typeSelect = '';
    $scope.indexSelect = '';
    $scope.add = function(index) {
      $scope.typeSelect = index;
      $scope.indexSelect = -1;
      // $('#uploaderHouseFileInput').click();              
      //angular.element('#uploaderHouseFileInput').click();  
      document.getElementById('uploaderHouseFileInput').click();
    }
    $scope.edit = function(index, indexParent) {
        $scope.typeSelect = indexParent;
        $scope.indexSelect = index;
        document.getElementById('uploaderHouseFileInput').click();
      }
      // $scope.readFile = function(){
      //          $scope.selectedFile = changeEvent.target.files[0];
      //         console.log( 1111);
      //         setTimeout("scope.uploader.uploadAll(); ",20);      
      // }
    var uuid4 = function() {
      return Math.floor((1 + Math.random()) * 0x10000)
        .toString(16)
        .substring(1);
    }

    var uuid = function() {
      return uuid4() + uuid4() + uuid4() + uuid4() + uuid4() + uuid4() + uuid4() + uuid4();
    }

    $scope.readFile = function(changeEvent) {
      var typeTure = true,
        sizeTrue = true;
      var str = 'image';

      for (var i = 0; i < 100; i++) {
        var fileGet = changeEvent.target.files[i];
        if (fileGet) {
          if (fileGet.type.toLowerCase().indexOf(str) === -1) {
            typeTure = false;
            break;
          }
          // if (fileGet.size > 0.3 * 1024*1024) { 【1】
          if (fileGet.size > 3 * 1024 * 1024 && sizeTrue) {
            sizeTrue = false;
            alert('上传的图片文件大小超过3M，可能会影响上传速度');
            // break; 【1】
          }
        } else {
          break;
        }
      }
      // if(typeTure && sizeTrue){ 【1】
      if (typeTure) {
        setTimeout("$('#itemUpload').click();", 20);
      } else if (!typeTure) {
        $('#uploaderHouseFileInput').val("");
        alert('上传的图片文件不能包含其他格式文件')
      }
      // else if (!sizeTrue) {
      //      $('#uploaderHouseFileInput').val("");   
      //     alert('上传的图片文件超过 3M 可能会影响上传速度');
      // }           
    }

  }]);